﻿/*=================================================================
* 版权信息：Copyright (C) 2005-2011 捷点科技 All Rights Reserved.
* 文件名称：CNVP_Menu.cs
* 创建标识：曾乃强 15158756652 2011-03-16 02:03:19
* 修改标识：
* 功能描述：
===================================================================*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CNVP.Framework.Helper;
using CNVP.Config;

namespace CNVP.JShop.DAL
{
    /// <summary>
    /// 对*CNVP_Menu*操作数据库(针对SQL Server)的实现
    /// </summary>
    public class CNVP_Menu
    {
       private string Prefix = DBConfig.Prefix;
        #region DAL
        public int Insert(Model.CNVP_Menu obj)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into  " + Prefix + "Menu ([Depth],[MenuName],[Weight],[MenuUrl],[PreView])");
            sql.Append("values(@Depth,@MenuName,@Weight,@MenuUrl,@PreView)");
            
            SqlParameter[] pra = {
                            new SqlParameter("@Depth",obj.Depth),
							new SqlParameter("@MenuName",obj.MenuName),
							new SqlParameter("@Weight",obj.Weight),
							new SqlParameter("@MenuUrl",obj.MenuUrl),
							new SqlParameter("@PreView",obj.PreView)
                                 };
            return DbHelper.ExecuteNonQuery(sql.ToString(), pra);
        }

        public int Update(Model.CNVP_Menu obj)
        {
             StringBuilder sql = new StringBuilder();
             sql.Append("update " + Prefix + "Menu set [Depth] = @Depth,[MenuName] = @MenuName,[Weight] = @Weight,[MenuUrl] = @MenuUrl,[PreView] = @PreView where [ID] = @ID");

            SqlParameter[] pra = { 
                            new SqlParameter("@ID",obj.ID),
							new SqlParameter("@Depth",obj.Depth),
							new SqlParameter("@MenuName",obj.MenuName),
							new SqlParameter("@Weight",obj.Weight==0?DbHelper.GetMaxID("ID","" + Prefix + "Menu"):obj.Weight),
							new SqlParameter("@MenuUrl",obj.MenuUrl),
							new SqlParameter("@PreView",obj.PreView)
                                };
             return DbHelper.ExecuteNonQuery(sql.ToString(),pra);
        }

        public int Delete(Model.CNVP_Menu obj)
        {
           StringBuilder sql = new StringBuilder();
           sql.Append("delete from " + Prefix + "Menu where [ID] = @ID");
           SqlParameter[] pra = {
                                new SqlParameter("@ID",obj.ID),
						
                                };
           return DbHelper.ExecuteNonQuery(sql.ToString(),pra);
        }

        public List<Model.CNVP_Menu> LoadAll(string sqlWhere)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select [ID],[Depth],[MenuName],[Weight],[MenuUrl],[PreView] from " + Prefix + "Menu");
            if (sqlWhere.Trim() != "")
            {
                sql.Append(" where " + sqlWhere);
            }
            DataTable DT = DbHelper.ExecuteTable(sql.ToString());
            List<Model.CNVP_Menu> list = new List<Model.CNVP_Menu>();
            if (DT != null && DT.Rows.Count > 0)
            {
                foreach (DataRow dr in DT.Rows)
                {
                    Model.CNVP_Menu cnvp_menu = new Model.CNVP_Menu();
                    cnvp_menu.ID = dr["ID"] is DBNull ? 0 : (int)dr["ID"];
                    cnvp_menu.Depth = dr["Depth"] is DBNull ? String.Empty : (string)dr["Depth"];
                    cnvp_menu.MenuName = dr["MenuName"] is DBNull ? String.Empty : (string)dr["MenuName"];
                    cnvp_menu.Weight = dr["Weight"] is DBNull ? 0 : (int)dr["Weight"];
                    cnvp_menu.MenuUrl = dr["MenuUrl"] is DBNull ? String.Empty : (string)dr["MenuUrl"];
                    cnvp_menu.PreView = dr["PreView"] is DBNull ? 0 : (int)dr["PreView"];
                    list.Add(cnvp_menu);
                }
            }
            return list;

        }

        public Model.CNVP_Menu LoadById(int ID)
        {
            
            StringBuilder sql = new StringBuilder();
            sql.Append("select [Depth],[MenuName],[Weight],[MenuUrl],[PreView] from " + Prefix + "Menu");
            sql.Append(" where [ID] = @ID");
            SqlParameter[] pra = {
                                new SqlParameter("@ID",ID)
                                };
            DataTable DT = DbHelper.ExecuteTable(sql.ToString(),pra);
            Model.CNVP_Menu cnvp_menu = new Model.CNVP_Menu();
            if (DT != null && DT.Rows.Count > 0)
            {
                DataRow dr = DT.Rows[0];
                cnvp_menu.Depth = dr["Depth"] is DBNull ? String.Empty : (string)dr["Depth"];
                cnvp_menu.MenuName = dr["MenuName"] is DBNull ? String.Empty : (string)dr["MenuName"];
                cnvp_menu.Weight = dr["Weight"] is DBNull ? 0 : (int)dr["Weight"];
                cnvp_menu.MenuUrl = dr["MenuUrl"] is DBNull ? String.Empty : (string)dr["MenuUrl"];
                cnvp_menu.PreView = dr["PreView"] is DBNull ? 0 : (int)dr["PreView"];
            }
             return cnvp_menu;
        }
        #endregion

        public List<Model.CNVP_Menu> LoadAllPower(int Depth, int AdminRoleID)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("SELECT  c.ID, c.Depth, c.MenuName, c.Weight, c.MenuUrl, c.PreView,Case IsNull(u.MenuID,0) When 0 Then '' Else 'checked' End As Flag ");
            sql.Append("FROM " + Prefix + "Menu as c left join " + Prefix + "AdminRoleMenu as u on c.ID=u.MenuID and u.AdminRoleID=" + AdminRoleID + " where c.Depth =" + Depth + " order by c.Weight");
            DataTable DT = DbHelper.ExecuteTable(sql.ToString());
            List<Model.CNVP_Menu> list = new List<Model.CNVP_Menu>();
            if (DT != null && DT.Rows.Count > 0)
            {
                foreach (DataRow dr in DT.Rows)
                {
                    Model.CNVP_Menu cnvp_menu = new Model.CNVP_Menu();
                    cnvp_menu.ID = dr["ID"] is DBNull ? 0 : (int)dr["ID"];
                    cnvp_menu.Depth = dr["Depth"] is DBNull ? String.Empty : (string)dr["Depth"];
                    cnvp_menu.MenuName = dr["MenuName"] is DBNull ? String.Empty : (string)dr["MenuName"];
                    cnvp_menu.Weight = dr["Weight"] is DBNull ? 0 : (int)dr["Weight"];
                    cnvp_menu.MenuUrl = dr["MenuUrl"] is DBNull ? String.Empty : (string)dr["MenuUrl"];
                    cnvp_menu.PreView = dr["PreView"] is DBNull ? 0 : (int)dr["PreView"];
                    cnvp_menu.Flag = dr["Flag"] is DBNull ? String.Empty : (string)dr["Flag"];
                    list.Add(cnvp_menu);
                }
            }
            return list;

        }

        public DataTable LoadAll2(string sqlWhere)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select [ID],[Depth],[MenuName],[Weight],[MenuUrl],[PreView] from " + Prefix + "Menu");
            if (sqlWhere.Trim() != "")
            {
                sql.Append(" where " + sqlWhere);
            }
            return DbHelper.ExecuteTable(sql.ToString());
        }
    }
}

